<?php
namespace app\controller;

use think\Request;
use think\facade\Db;

class Smslog extends Common
{

	/**
	 * 短信发送记录
	 *
	 * @return
	 */
	public function read(Request $request)
	{
		if($this->hasPermission($request->sessInfo, 'smslog:read')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		$where = [];
		$limit = $request->param('limit/d', 20);
		$sort = $request->param('sort', 'id', 'trim');
		$order = $request->param('order', 'desc', 'trim');

		array_push( $where, ['companyId', '=', $request->sessInfo['companyId'] ] );

		$phone = $request->param('phone', '', 'trim');
		if($phone) {
			array_push( $where, ['phone', 'like',  "%{$phone}%" ] );
		}
		
		$status = $request->param('status', '', 'trim');
		if($status) {
			array_push( $where, ['status', '=',  $status ] );
		}

		$createTimeStart = $request->param('createTimeStart', '', 'trim');
		$createTimeEnd = $request->param('createTimeEnd', '', 'trim');
		if( $createTimeStart && $createTimeEnd ) {
			array_push( $where, ['datetime', 'between', cloud_datetime_format( $createTimeStart, $createTimeEnd, false) ] );
		} else {
			array_push( $where, ['datetime', 'between', cloud_datetime_format( date('Y-m-d 00:00:00'), date('Y-m-d 00:00:00', strtotime("+1 day")), false) ] );
		}

		$query = Db::name('cfg_smslog')->where($where)->order($sort, $order)->paginate($limit);

		return json(['code'=> 0, 'msg'=> '', 'count'=> $query->total(), 'data'=> $query->items() ]);
	}

}
